import { createRouter, createWebHashHistory } from 'vue-router'
import BasicLayout from "@/layouts/BasicLayout.vue";
import OrderPage from '@/pages/order/OrderPage.vue';
import { message } from 'ant-design-vue';
import {useAdminLoginStore} from "@/store/useAdminLoginUserStore";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/order'
    },
    {
      path: '/order',
      name: 'order',
      component: OrderPage
    },
    {
      path: '/my-orders',
      name: 'myOrders',
      component: () => import('@/pages/order/MyOrdersPage.vue')
    },
    {
      path: '/adLogin',
      name: 'adminLogin',
      component: () => import('@/pages/admin/login/AdminLoginPage.vue')
    },
    {
      path: '/empty',
      name: 'empty',
      component: () => import('@/pages/admin/empty/NoPermissionPage.vue')
    },
    {
      path: '/admin',
      component: BasicLayout,
      children: [
        {
          path: '',
          redirect: '/admin/adminHome'
        },
        {
          path: 'adminHome',
          name: 'adminHome',
          component: () => import('@/pages/admin/AdminHomePage.vue')
        },
        {
          path: 'store',
          name: 'store',
          component: () => import('@/pages/admin/store/StoreListPage.vue')
        },
        {
          path: 'category',
          name: 'category',
          component: () => import('@/pages/admin/category/CategoryListPage.vue')
        },
        {
          path: 'product',
          name: 'product',
          component: () => import('@/pages/admin/product/ProductListPage.vue')
        },
        {
          path: 'product-config',
          name: 'product-config',
          component: () => import('@/pages/admin/product/ProductConfigPage.vue')
        },
        {
          path: 'param',
          name: 'param',
          component: () => import('@/pages/admin/param/ParameterView.vue')
        },
        {
          path: 'template',
          name: 'template',
          component: () => import('@/pages/admin/template/TemplateListPage.vue')
        },
        {
          path: 'store-orders',
          name: 'storeOrders',
          component: () => import('@/pages/admin/store-orders/StoreOrdersPage.vue')
        },
        {
          path: 'permission',
          name: 'permission',
          component: () => import('@/pages/admin/permission/PermissionListPage.vue')
        },
        {
          path: 'employee',
          name: 'employee',
          component: () => import('@/pages/admin/user/UserListPage.vue')
        },
        {
          path: 'role',
          name: 'role',
          component: () => import('@/pages/admin/role/RoleListPage.vue')
        }
      ]
    },
    // 重定向规则
    {
      path: '/store',
      redirect: '/admin/store'
    },
    {
      path: '/category',
      redirect: '/admin/category'
    },
    {
      path: '/product',
      redirect: '/admin/product'
    },
    {
      path: '/product-config',
      redirect: '/admin/product-config'
    },
    {
      path: '/param',
      redirect: '/admin/param'
    },
    {
      path: '/template',
      redirect: '/admin/template'
    },
    {
      path: '/permission',
      redirect: '/admin/permission'
    },
    {
      path: '/employee',
      redirect: '/admin/employee'
    },
    {
      path: '/role',
      redirect: '/admin/role'
    }
  ]
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // 检查是否是管理员路由
  if (to.path.startsWith('/admin')) {
    const adminLoginStore = useAdminLoginStore();
    console.log(1);
    try {
      // 获取最新的登录状态
      await adminLoginStore.fetchAdminLoginUser();
      console.log(2);
    } catch (error) {
      // 重定向到管理端登录页
      next({ path: '/adLogin', replace: true });
      return;
    }

    // 未登录
    if (!adminLoginStore.loginUser.userId) {
      // 未登录，重定向到管理端登录页
      message.warning('请先登录');
      // router.push('/adLogin');
      next({ path: '/adLogin', replace: true });
      return;
    }
  }

  // 其他情况正常放行
  next();
});

export default router
